package com.chengcang.admin.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.admin.modules.sys.dao.SysOrderProductTypeSnapshotDao;
import com.chengcang.admin.modules.sys.dao.SysOrderProductSnapshotDao;
import com.chengcang.admin.modules.sys.dto.orderSnapshotDTO.SysOrderProductSnapshotDTO;
import com.chengcang.admin.modules.sys.entity.orderSnapshotEntity.SysOrderProductSnapshotEntity;
import com.chengcang.admin.modules.sys.service.SysDictDataService;
import com.chengcang.admin.modules.sys.service.SysOrderProductSnapshotService;
import com.chengcang.admin.modules.sys.service.SysProductTypeService;
import com.chengcang.common.service.impl.CrudServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-01-10
 */
@Service
@Slf4j
public class SysOrderProductSnapshotServiceImpl extends CrudServiceImpl<SysOrderProductSnapshotDao, SysOrderProductSnapshotEntity, SysOrderProductSnapshotDTO> implements SysOrderProductSnapshotService {

    @Autowired
    private SysOrderProductSnapshotDao sysOrderProductSnapshotDao;

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysProductTypeService sysProductTypeService;

    @Autowired
    private SysOrderProductTypeSnapshotDao sysOrderProductTypeSnapshotDao;

    private static final String LEVEL = "level";

    private static final String PRODUCT_TYPE_ID = "product_type_id";

    @Override
    public QueryWrapper<SysOrderProductSnapshotEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get(PRODUCT_TYPE_ID);
        QueryWrapper<SysOrderProductSnapshotEntity> wrapper = new QueryWrapper<>();
        //二级
        if (params.get(LEVEL) != null && params.get(LEVEL).equals("1")) {
            wrapper.eq(StringUtils.isNotBlank(id), PRODUCT_TYPE_ID, id);
        } else if (params.get(LEVEL) != null && params.get(LEVEL).equals("0")) {
            List<Long> secondIdsById = sysProductTypeService.getSecondIdsById(Long.parseLong(id));
            if(secondIdsById.size() == 0){
                return null;
            }
            wrapper.in(secondIdsById.size() != 0, PRODUCT_TYPE_ID, secondIdsById);
        }
        return wrapper;
    }





}
